package com.qk.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qk.common.PageResult;
import com.qk.entity.Dept;
import com.qk.mapper.DeptMapper;
import com.qk.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * ClassName: DeptServiceImpl
 * Package: com.qk.service.impl
 * Description:
 *
 * @Author zt
 * @Create 2025/7/27 9:10
 * @Version 1.0
 */
@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public void add(Dept dept) {

        //补全基础属性
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());

        //调用mapper
        deptMapper.add(dept);

    }

    @Override
    public PageResult<Dept> listDepts(String name, Integer status, Integer page, Integer pageSize) {
        // 使用PageHelper进行分页查询
        PageHelper.startPage(page, pageSize);
        List<Dept> depts = deptMapper.selectByCondition(name, status);
        PageInfo<Dept> pageInfo = new PageInfo<>(depts);

        // 构建分页结果
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public Dept getById(Integer id) {
         return deptMapper.selectById(id);
    }

    @Override
    public void update(Dept dept) {
        //补全属性：更新时间
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);
    }

    @Override
    public void delete(Integer id) {
        deptMapper.delete(id);
    }

    @Override
    public List<Dept> list() {
        //返回的是一个Dept的集合
        return deptMapper.list();
    }
}
